package com.gemme.gemme.wode;

import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.manager.DataPares;
import com.gemme.gemme.util.PCUtil;
import com.gemme.gemme.view.GMPhotoView;
import com.gemme.gemme.view.GoodsSatateDialog;
import com.gemme.gemme.view.OnChangeLIstener;
import com.gemme.gemme.view.SgDDView;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvTvArrow;
import com.gemme.gemme.view.UpPIcView;
import com.jaeger.library.StatusBarUtil;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.RequiresApi;

//侧滑删除地址
public class RefundActivity extends BaseActivity{
    public static final int type_no_good = 0;
    public static final int type_has_good = 1;

    int type = type_no_good;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        initTop();
        initView();

        initIntentData();

    }

    int id;
    private void initIntentData() {
        try {

            id = getIntent().getIntExtra("data",-1);
            type = getIntent().getIntExtra("type",type_no_good);
            if(id > -1){
                getData();
            }

           changeByType();
        }catch (Exception e){

        }
    }

    private void changeByType() {
        if(type == type_no_good){
            hwzt.setRightText("未收到货");
        }else{
            hwzt.setRightText("已收到货");
        }
    }


    private void getData() {
        String url = DataManager.BaseUrl_panghu + "mall/shoporderdetail/" + id;



        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {

                    JSONObject jsonObject = new JSONObject(response);

                    String data = jsonObject.getJSONObject("data").getString("wholeOrderVo");
                    JSONObject jbInfo = jsonObject.getJSONObject("data").getJSONObject("paddressResquersVo");
                    DingdanListBean.DataBean dataBean = JSON.parseObject(data, DingdanListBean.DataBean.class);
                    String addressee = jbInfo.getString("addressee");
                    String address = jbInfo.getString("address");
                    sgDDView.setdata(dataBean);
                    tv_price.setText("退款金额:" + dataBean.getPaymentMoney() + "");
                    tv_desc.setText("不可修改，最多¥"+ dataBean.getPaymentMoney() +"，含发货邮费¥8.00");
                   /* tv_name.setText(addressee + "");
                    tv_address.setText(address + "");
                    sgDDView.setdata(dataBean);
                    tv_order_id.setText(dataBean.getOrderId() + "");
                    tv_create_time.setText(dataBean.getCreateTime() + "");
                    tv_brand.setText(dataBean.getBrandName());*/

                    System.out.println("");
                }catch (Exception e){

                }

            }
        });
    }

    SgDDView sgDDView;
    TvTvArrow hwzt;
    TextView tv_price;
    TextView tv_desc;
    UpPIcView upPIcView;
    GMPhotoView photoView;
    TextView tv_submit;
    EditText editV;
    private void initView() {

        hwzt = findViewById(R.id.hwzt);
        sgDDView = findViewById(R.id.sgDDView);
        hwzt.setLeftText("货物状态").setRightText("请选择");

        tv_price = findViewById(R.id.tv_price);
        tv_desc = findViewById(R.id.tv_desc);

        upPIcView = findViewById(R.id.upPIcView);
        tv_submit = findViewById(R.id.tv_submit);

        editV = findViewById(R.id.editV);
        photoView = findViewById(R.id.photoView);
        photoView.setChooseMode(PictureMimeType.ofImage());
        hwzt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                GoodsSatateDialog dialog = new GoodsSatateDialog();
                dialog.showDialog(mContext);
                dialog.setOnChangeLIstener(new OnChangeLIstener() {
                    @Override
                    public void onChangeLIstener() {
                        boolean state = dialog.getState();
                        type =  state ? type_has_good : type_no_good;
                        changeByType();
                    }
                });
            }
        });


        upPIcView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });

        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toserver();
            }
        });
    }

    private void toserver() {

        if(id < 0){
            Toast.makeText(mContext, "提交失败", Toast.LENGTH_SHORT).show();
        }
      /*  if(type == type_no_good){
            hwzt.setRightText("未收到货");
        }else{
            hwzt.setRightText("已收到货");
        }*/

        String url = DataManager.BaseUrl + "mall/shoporderdetail";

        RefundBean refundBean = new RefundBean();


        String remboursement = "1{货物状态：" + hwzt.getRightText() + "}" +
                "2{退款原因:" + editV.getText() + "}" +
                "3{上传凭证图片OSS地址:" +
                "https://gemme.oss-cn-hangzhou.aliyuncs.com/backstage/2020-08-2375742cd3-e213-4e76-8966-8dd3c475ab84_4.png;https://gemme.oss-cn-hangzhou.aliyuncs.com/backstage/2020-08-103ac96823-67fe-49a4-8266-a1a8affa586a_skii6.jpg"
                + "}";

        refundBean.setId(id + 0L);
        refundBean.setOrderStatus(5L);
        refundBean.setRemboursement(remboursement);

        String jsonString = JSON.toJSONString(refundBean);

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.PUT_TYPE, jsonString, headMap, new DataManager.GeResponse() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onResponse(String response) {
                try {

                    System.out.println("");
                    boolean sucess = DataPares.isSucess(mContext, response);
                    if(sucess){
                        finish();
                    }
                }catch (Exception e){

                }

            }
        });
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("申请退款");
    }




}